package com.shy.transaction.dao.impl;

import com.shy.transaction.dao.IUserDao;
import com.shy.transaction.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Repository
public class UserDao implements IUserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public Integer insert(String username) {
        return jdbcTemplate.update("insert into t_user(username) values(?)", username);
    }

    @Override
    public Integer update(Integer id, String username) {
        String sql = "update t_user set username=? where id=?";
        return jdbcTemplate.update(sql, username, id);
    }

    @Override
    public User queryById(Integer id) {
        String sql = "select * from t_user where id=?";
        User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), id);
        System.out.println(user);
        return user;
    }

    @Override
    public Integer delete(Integer id) {
        String sql = "delete from t_user where id=?";
        Integer result = jdbcTemplate.update(sql, id);
        return result;
    }
}
